﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using CarlosAg.ExcelXmlWriter;

namespace BangkokSteel
{
    public partial class frmPurchaseOrder : Form, MainFormListener
    {
        private TabControl mainTab;
        private MySqlConnection conn = null;
        private MySqlDataAdapter adapter = null;
        private string MODE = "";

        public frmPurchaseOrder(TabControl mainTab)
        {
            this.mainTab = mainTab;
            InitializeComponent();
        }

        #region Form Event

        private void frmPurchaseOrder_Load(object sender, EventArgs e)
        {       
            conn = new MySqlConnection(BangkokSteel.Properties.Settings.Default.bangkoksteelConnectionString);

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            adapter = new MySqlDataAdapter("SELECT * FROM purchase_order", conn);
            adapter.Fill(dataSet1, "purchase_order");

            dataGridView1.DataSource = dataSet1.Tables["purchase_order"];

            SetCbo();

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.mainTab.TabPages.Remove(this.mainTab.SelectedTab);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(false);

            MODE = "ADD";

            ClearFields();

            panel1.Visible = true;

            //txtName.Focus();
            //txtName.Select();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
            {
                EnableOrDisableMainPage(false);

                DataGridViewRow row = dataGridView1.SelectedRows[0];

                MODE = "EDIT";

                //txtId.Text = row.Cells["id"].Value.ToString();
                //txtName.Text = row.Cells["name"].Value.ToString();
                //txtValue.Text = row.Cells["value"].Value.ToString();


                panel1.Visible = true;

                //txtName.Focus();
                //txtName.SelectAll();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

        }

        private void frmPurchaseOrder_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        btnClose_Click(null, null);
                    }
                    break;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //if (txtName.Text.Trim().Length == 0)
                //{
                //    MessageBox.Show("กรุณาเลือกใส่ชื่อให้เรียบร้อย");

                //    txtName.Focus();
                //    txtName.SelectAll();

                //    return;
                //}

                MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);

                if ("ADD".Equals(MODE))
                {
                    MySqlCommand cmd = (MySqlCommand)builder.GetInsertCommand(true);
                    cmd.Parameters.Clear();
                    //cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtName.Text;
                    //cmd.Parameters.Add("@value", MySqlDbType.Double).Value = txtValue.Text;

                    int affectedRow = cmd.ExecuteNonQuery();

                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    ClearFields();

                    //txtName.Focus();
                    //txtName.SelectAll();

                }
                else if ("EDIT".Equals(MODE))
                {
                    string sql = @"UPDATE `vat` SET `name` = @name, `value` = @value WHERE (`id` = @Original_id)";

                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Clear();
                    //cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtName.Text;
                    //cmd.Parameters.Add("@value", MySqlDbType.Double).Value = txtValue.Text;
                    //cmd.Parameters.Add("@Original_id", MySqlDbType.Int32).Value = txtId.Text;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                    panel1.Visible = false;
                    ClearFields();

                    MODE = "";
                }
                // Refresh for New Data
                dataSet1.Tables["Vat"].Clear();
                adapter.Fill(dataSet1, "Vat");

                EnableOrDisableMainPage(true);

            }
            catch (FormatException ex)
            {
                MessageBox.Show("ใส่ค่าคำนวณผิด กรุณาใส่เป็นตัวเลขเท่านั้น");
                //txtValue.Focus();
                //txtValue.SelectAll();
            }
        }

        private void btnOK_Add_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(true);

            panel1.Visible = false;
            MODE = "";
            ClearFields();
        }

        #endregion

        #region Function

        private void EnableOrDisableMainPage(bool isEnable)
        {
            btnNew.Enabled = isEnable;
            btnAdd.Enabled = isEnable;
            btnEdit.Enabled = isEnable;
            btnDelete.Enabled = isEnable;
            btnClose.Enabled = isEnable;
            dataGridView1.Enabled = isEnable;
        }

        private void ClearFields()
        {
            //txtId.Text = "";
            //txtName.Text = "";
            //txtValue.Text = "";
        }

        private void SetCbo()
        {
            //Supplier Type
            //MySqlDataAdapter adapter2 = new MySqlDataAdapter("SELECT * FROM supplier_type", conn);
            //adapter2.Fill(dataSet1, "supplier_type");
            //cboSupplierType.DataSource = dataSet1.Tables["supplier_type"];
            //cboSupplierType.DisplayMember = "name";
            //cboSupplierType.ValueMember = "id";

            ////Payment Term
            //MySqlDataAdapter adapter3 = new MySqlDataAdapter("SELECT * FROM payment_term", conn);
            //adapter3.Fill(dataSet1, "payment_term");
            //cboPaymentTerm.DataSource = dataSet1.Tables["payment_term"];
            //cboPaymentTerm.DisplayMember = "name";
            //cboPaymentTerm.ValueMember = "id";

            ////Receive Condition
            //MySqlDataAdapter adapter4 = new MySqlDataAdapter("SELECT * FROM received_condition", conn);
            //adapter4.Fill(dataSet1, "received_condition");
            //cboReceiveCon.DataSource = dataSet1.Tables["received_condition"];
            //cboReceiveCon.DisplayMember = "name";
            //cboReceiveCon.ValueMember = "id";

            ////VAT
            //MySqlDataAdapter adapter5 = new MySqlDataAdapter("SELECT * FROM vat", conn);
            //adapter5.Fill(dataSet1, "vat");
            //cboVat.DataSource = dataSet1.Tables["vat"];
            //cboVat.DisplayMember = "name";
            //cboVat.ValueMember = "id";
        }

        private bool Validation()
        {
            //if (txtName.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("กรุณาเลือกใส่ชื่อให้เรียบร้อย");

            //    txtName.Focus();
            //    txtName.SelectAll();

            //    return false;
            //}

            //if (txtOwner.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("กรุณาเลือกใส่ชื่อเจ้าของให้เรียบร้อย");

            //    txtOwner.Focus();
            //    txtOwner.SelectAll();

            //    return false;
            //}

            //if (txtAddress.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("กรุณาเลือกใส่ที่อยู่ให้เรียบร้อย");

            //    txtAddress.Focus();
            //    txtAddress.SelectAll();

            //    return false;
            //}

            //if (cboSupplierType.SelectedValue == null)
            //{
            //    MessageBox.Show("กรุณาเลือก Supplier Type ให้เรียบร้อย");
            //    return false;
            //}

            //if (txtCredit.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("กรุณาเลือกใส่เครดิตให้เรียบร้อย");

            //    txtCredit.Focus();
            //    txtCredit.SelectAll();

            //    return false;
            //}

            //if (cboPaymentTerm.SelectedValue == null)
            //{
            //    MessageBox.Show("กรุณาเลือกกลุ่มการจ่ายให้เรียบร้อย");
            //    return false;
            //}

            //if (cboReceiveCon.SelectedValue == null)
            //{
            //    MessageBox.Show("กรุณาเลือกกลุ่มเงื่อนไขการรับสินค้าให้เรียบร้อย");
            //    return false;
            //}

            //if (cboVat.SelectedValue == null)
            //{
            //    MessageBox.Show("กรุณาเลือก VAT ให้เรียบร้อย");
            //    return false;
            //}

            return true;
        }

        #endregion

        #region Interface

        void MainFormListener.onMainFormClose()
        {
            // TODO
        }

        public void onTabPageAdded()
        {
            // TODO
        }

        #endregion













    }
}
